package com.kaibes.core.base.util;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class NullUtils {

    public static String stringNotNull(String source) {
        return source==null?"":source;
    }
    
    public static String stringNotNull(String source, String str) {
        return source==null?str:source;
    }
    
    public static void null2Value(List<Object> datas, Object value, String... fieldNames) {
        if (!datas.isEmpty()) {
            Class<?> clazz = datas.get(0).getClass();
            try {
                List<Field> fieldList = new ArrayList<>();
                for (String fieldName : fieldNames) {
                    fieldList.add(clazz.getDeclaredField(fieldName));
                }
                for (Object obj : datas) {
                    try {
                        for (Field field : fieldList) {
                            field.setAccessible(true);
                            if (field.get(obj) == null) {
                                field.set(obj, value);
                            }
                        }
                    } catch (IllegalArgumentException | IllegalAccessException e) {
                        throw new RuntimeException(e);
                    }
                }
            } catch (NoSuchFieldException | SecurityException e1) {
                return;
            }
        }
    }
    
    public static void zero2Null(List<Object> datas, String... fieldNames) {
        if (!datas.isEmpty()) {
            Class<?> clazz = datas.get(0).getClass();
            try {
                List<Field> fieldList = new ArrayList<>();
                for (String fieldName : fieldNames) {
                    fieldList.add(clazz.getDeclaredField(fieldName));
                }
                for (Object obj : datas) {
                    try {
                        for (Field field : fieldList) {
                            field.setAccessible(true);
                            Object value = field.get(obj);
                            if (String.valueOf(value).equals("0")) {
                                field.set(obj, null);
                            }
                        }
                    } catch (IllegalArgumentException | IllegalAccessException e) {
                        throw new RuntimeException(e);
                    }
                }
            } catch (NoSuchFieldException | SecurityException e1) {
                return;
            }
        }
    }
    
    public static void null2Value(List<Object> datas, Object value) {
        if (!datas.isEmpty()) {
            Class<?> clazz = datas.get(0).getClass();
            List<Field> fieldList = ClassUtils.getIntegerFieldList(clazz);
            for (Object obj : datas) {
                for (Field field : fieldList) {
                    field.setAccessible(true);
                    try {
                        if (field.get(obj) == null) {
                            field.set(obj, value);
                        }
                    } catch (IllegalArgumentException | IllegalAccessException e) {
                        return;
                    }
                }
            }
        }
    }
    
    public static void null2Empty(List<Object> datas) {
        if (!datas.isEmpty()) {
            Class<?> clazz = datas.get(0).getClass();
            List<Field> fieldList = ClassUtils.getStringFieldList(clazz);
            for (Object obj : datas) {
                for (Field field : fieldList) {
                    field.setAccessible(true);
                    try {
                        if (field.get(obj) == null) {
                            field.set(obj, "");
                        }
                    } catch (IllegalArgumentException | IllegalAccessException e) {
                        return;
                    }
                }
            }
        }
    }
    
}
